package com.yto.hrreport.event.buttons
{
	import com.adobe.serialization.json.JSON;
	import com.yto.hrreport.event.buttons.superbutton.ButtonEvent;
	import com.yto.hrreport.pub.IProcessor;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.HDividedBox;
	import mx.containers.Panel;
	import mx.controls.CheckBox;
	import mx.controls.TextInput;
	import mx.controls.Tree;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class RefreshButtonEvent extends ButtonEvent {
		
		private static var refreshEvent : RefreshButtonEvent = new RefreshButtonEvent();
		private static var parentName : String;
		private var sysTree : Tree;
		public function RefreshButtonEvent() {
			super();
		}
		
		public static function getInstance(parentName : String) : RefreshButtonEvent {
			RefreshButtonEvent.parentName = parentName;
			return refreshEvent;
		}
		
		public function refreshEvent() : void {
			
			var parentCanvas : Canvas = Canvas(hrreport.ui.mainNavigator.getChildByName(parentName + "_canvas"));
			var headCanvas : Canvas = Canvas(parentCanvas.getChildByName(parentName + "_headCanvas"));
			var bodyCanvas : Canvas = Canvas(parentCanvas.getChildByName(parentName + "_bodyCanvas"));
			var hdivBox : HDividedBox = HDividedBox(bodyCanvas.getChildByName(parentName + "_hdivBox"));
			sysTree = Tree(hdivBox.getChildByName(parentName + "_sysTree"));
			var sysPanel : Panel = Panel(hdivBox.getChildByName(parentName + "_sysFunc"));
			
			if(sysPanel == null) {
				sysPanel  = Panel(hdivBox.getChildByName(parentName + "_sysTemplate"));
			} else {
				var sysCanvas : Canvas = Canvas(sysPanel.getElementAt(0));
				
				TextInput(sysCanvas.getChildByName(parentName + "_txtNodeCode")).text = null;
				TextInput(sysCanvas.getChildByName(parentName + "_txtNodeId")).text = null;
				TextInput(sysCanvas.getChildByName(parentName + "_txtNodeName")).text = null;
				TextInput(sysCanvas.getChildByName(parentName + "_txtParentNodeCode")).text = null;
				CheckBox(sysCanvas.getChildByName(parentName + "_chkIsParent")).selected = false;
				
			}
			
			var nodeServer : RemoteObject = new RemoteObject;
			nodeServer.destination = IProcessor.NodeProcessor;
			nodeServer.resultNodeJSON("1");
			nodeServer.addEventListener(ResultEvent.RESULT , sysChildResultHandler);
			
		}
		
		public function sysChildResultHandler(event : ResultEvent) : void {
			
			var lastResult : String = event.result as String;
			
			if(lastResult != null ) {
				var result : Array = JSON.decode(lastResult) as Array;
				var replaceResult : Array = new Array();
				for each(var item : Object in result) {
					
					if(String(item.nodeid).split(".")[0] == "system")
						continue;
					
					if(item.isparent == "Y" && item.children == null)
						item.children = "[]";
					
					replaceResult.push(item);
				}
				
				var nodeArray : ArrayCollection = new ArrayCollection(replaceResult);
				sysTree.dataProvider = nodeArray;
				
			} 
			
		}
		
	}
}